﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.Common;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Reflection;
using System.Xml;
using System.Web;
//using System.Data.OracleClient;
namespace BobbyCore
{
    /// <summary>
    /// 移植到的数据库名称集
    /// </summary>
    public enum DataBaseType
    {
        SqlServer,
        Access,
        Oracle,
    }
    public class MyCore
    {
        public MyCore()
        {

        }
        /// <summary>
        /// 创建并初始化Connection连接实例
        /// </summary>
        /// <param name="dbType">数据提供程序类型</param>
        /// <returns>返回一个已经创建好的数据库连接实例</returns>
        public static DbConnection CreateConnection(DataBaseType dbType) 
        {
            DbConnection con;
            switch (dbType)
            {
                case DataBaseType.Access:
                    con = new OleDbConnection(ConfigurationManager.ConnectionStrings["AccessConn"].ConnectionString);
                    break;
                default:
                    con = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConn"].ConnectionString);
                    break;
            }
            return con;
        }
        /// <summary>
        /// 创建并初始化Command实例(SQL语句中带参数)
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="text"></param>
        /// <param name="pars"></param>
        /// <param name="dbType"></param>
        /// <param name="con"></param>
        /// <returns></returns>
        public static DbCommand CreateCommand(CommandType Type, string text, DbParameter[] pars, DataBaseType dbType, DbConnection con)
        {

            DbCommand cmd;
            switch (dbType)
            {
                case DataBaseType.Access:
                    cmd = new OleDbCommand();
                    cmd.CommandType = Type;
                    cmd.CommandText = text;
                    cmd.Connection = (OleDbConnection)con;
                    foreach (DbParameter dbpar in pars)
                    {
                        cmd.Parameters.Add((OleDbParameter)dbpar);
                    }
                    break;
                default:
                    cmd = new SqlCommand();
                    cmd.CommandType = Type;
                    cmd.CommandText = text;
                    cmd.Connection = (SqlConnection)con;
                    foreach (DbParameter dbpar in pars)
                    {
                        cmd.Parameters.Add((SqlParameter)dbpar);
                    }
                    break;
            }
            return cmd;
        }
        /// <summary>
        /// 创建并初始化Command实例(SQL语句中无参数)
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="text"></param>
        /// <param name="dbType"></param>
        /// <param name="con"></param>
        /// <returns></returns>
        public static DbCommand CreateCommand(CommandType Type, string text, DataBaseType dbType, DbConnection con)
        {

            DbCommand cmd;
            switch (dbType)
            {
                case DataBaseType.Access:
                    cmd = new OleDbCommand();
                    cmd.CommandType = Type;
                    cmd.CommandText = text;
                    cmd.Connection = (OleDbConnection)con;

                    break;
                default:
                    cmd = new SqlCommand();
                    cmd.CommandType = Type;
                    cmd.CommandText = text;
                    cmd.Connection = (SqlConnection)con;
                    break;
            }
            return cmd;


        }
        /// <summary>
        /// 创建并初始化一个DataAdapter对象
        /// </summary>
        /// <param name="cmd">Command实例对象</param>
        /// <param name="dbType">数据提供程序类型</param>
        /// <returns>返回一个已经创建好的DataAdapter对象</returns>
        public static DbDataAdapter CreateDataAdapter(DbCommand cmd, DataBaseType dbType)
        {
            DbDataAdapter ida;
            switch (dbType)
            {
                case DataBaseType.Access:
                    ida = new OleDbDataAdapter((OleDbCommand)cmd);
                    break;
                default:
                    ida = new SqlDataAdapter((SqlCommand)cmd);
                    break;
            }
            return ida;
        }
    }
}
